from appServerCode.models import ptUser
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication

class Authentication(BaseAuthentication):

    '''
    全局接口过滤
    '''
    def authenticate(self,request):
        # 首先验证用户传递的token值和数据库中的是否一致
        token = request.META.get('HTTP_TOKEN')
        uuid=request.META.get('HTTP_UUID')
        print('进入认证')
        print('进入认证，数据data：',token,uuid)
        if not token:
            print('认证失败')
            raise exceptions.AuthenticationFailed('认证失败')
        else:
            token_obj = ptUser.objects.filter(token=token,user_id=uuid)
            if not token_obj:
                print('token认证失败')
                raise exceptions.AuthenticationFailed('token认证失败')
            else:
                print('认证成功')
                return (token_obj[0],token_obj[0].token)

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        # 验证失败时，返回的响应头WWW-Authenticate对应的值
        pass
